package com.ykdl.app.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.ykdl.app.R;

public class SlipButton extends View implements OnTouchListener {
	private String strName;
	private boolean enabled = true;
	public boolean flag = false;
	public boolean NowChoose = false;
	private boolean OnSlip = false;
	private boolean isChgLsnOn = false;
	public float DownX = 0f, NowX = 0f;
	private Rect Btn_On, Btn_Off;
	private Bitmap bg_on, bg_off, slip_btn, open_btn;
	public String key = null;
	private OnChangedListener chgLsn;

	public SlipButton(Context context) {
		super(context);
		init();
	}

	public SlipButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public void setChecked(boolean fl) {
		if (fl) {
			flag = true;
			NowChoose = true;
			NowX = bg_on.getWidth();
		} else {
			flag = false;
			NowChoose = false;
			NowX = 0;
		}
	}

	public void setEnabled(boolean b) {
		if (b) {
			enabled = true;
		} else {
			enabled = false;
		}
	}

	public void setKey(String name) {
		key = name;
	}

	private void init() {
		try {
			bg_on = BitmapFactory.decodeResource(getResources(),
					R.drawable.on_btn);
			bg_off = BitmapFactory.decodeResource(getResources(),
					R.drawable.off_btn);
			slip_btn = BitmapFactory.decodeResource(getResources(),
					R.drawable.white_btn);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());

		Btn_Off = new Rect(bg_off.getWidth() - slip_btn.getWidth(), 0,
				bg_off.getWidth(), slip_btn.getHeight());
		setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		float x;
		{
			if (flag) {
				NowX = bg_on.getWidth();
				flag = false;
			}
			if (NowX < (bg_on.getWidth() / 2)) {
				canvas.drawBitmap(bg_off, matrix, paint);
			} else {
				canvas.drawBitmap(bg_on, matrix, paint);
			}
			if (OnSlip) {
				if (NowX >= bg_on.getWidth())
					x = bg_on.getWidth() - slip_btn.getWidth() / 2;
				else
					x = NowX - slip_btn.getWidth() / 2;
			} else {
				if (NowChoose)
					x = Btn_Off.left;
				else
					x = Btn_On.left;
			}
			if (x < 0) {
				x = 0;
			} else if (x > bg_on.getWidth() - slip_btn.getWidth())
				x = bg_on.getWidth() - slip_btn.getWidth();
			canvas.drawBitmap(slip_btn, x, 0, paint);
		}
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (!enabled) {
			return false;
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_MOVE:
			NowX = event.getX();
			break;
		case MotionEvent.ACTION_DOWN:
			if (event.getX() > bg_on.getWidth()
					|| event.getY() > bg_on.getHeight())
				return false;
			OnSlip = true;
			DownX = event.getX();
			NowX = DownX;
			break;
		case MotionEvent.ACTION_UP:
			OnSlip = false;
			boolean LastChoose = NowChoose;
			if (event.getX() >= (bg_on.getWidth() / 2)) {
				NowChoose = true;
			} else {
				NowChoose = false;
			}
			if (isChgLsnOn )// 如果设置了监听器,就调用其方法..
				chgLsn.OnChanged(NowChoose);
			break;
		default:

		}
		invalidate();
		return true;
	}

	public void SetOnChangedListener(OnChangedListener l) {// 设置监听器,当状态修改的时候
		isChgLsnOn = true;
		chgLsn = l;
	}

	// 滑动监听
	public interface OnChangedListener {
		abstract void OnChanged(boolean checkState);
	}
}
